﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Timers;
using System.Media;
using System.IO.Ports;

namespace PROJECT_Wekker
{
    public partial class Form1 : Form
    {
        Boolean alarmOnOff;
        String setTime;
        String current;
        DateTime timer;
        SoundPlayer player;
        FileDialog files;
        int plusOne;
       

        public Form1()
        {
            InitializeComponent();
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.Open(); 

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }

        // load and set all to default
        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "OFF";
            button1.Enabled = false;
            button1.BackColor = Color.Red;
            alarmOnOff = false;
            current = DateTime.Now.ToShortTimeString();
            textBox3.Text = current;
            timer1.Enabled = true;
            timer2.Enabled = true;
            textBox1.Text = "C:/WAKEUP.wav";
            checkBox1.Checked = true;
            textBox2.Visible = false;
            textBox1.Enabled = false;
            
            



        }

        // Set alarm
        private void button2_Click(object sender, EventArgs e)
        {
            alarmOnOff = true;
            button1.Text = "ON";
            button1.BackColor = Color.Green;
            timer = dateTimePicker1.Value;
            setTime = timer.ToShortTimeString();
            textBox2.Text = setTime;
            timer2.Enabled = true;
            plusOne = dateTimePicker1.Value.Minute;

            
        }

        // reset ALL
        private void button3_Click(object sender, EventArgs e)
        {
            button1.Text = "OFF";
            button1.Enabled = false;
            button1.BackColor = Color.Red;
            alarmOnOff = false;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {

            current = DateTime.Now.ToShortTimeString();
            textBox3.Text = current;

        }

        private void timer2_Tick(object sender, EventArgs e)
        {

            if (alarmOnOff == true)
            {


                if (textBox2.Text == textBox3.Text)
                {
                    //start timer 3, for keeping lights on
                    if (checkBox2.Checked == true)
                    {
                        timer3.Enabled = false;
                        timer3.Enabled = true;
                    }

                    // if check then play
                    if (checkBox1.Checked == true && textBox1.Text != "")
                    {
                        SoundPlayer player = new SoundPlayer(textBox1.Text);
                        player.PlayLooping();
                        

                        timer2.Enabled = false;

                        serialPort1.Write("1");

                        if (MessageBox.Show("Are you ready or not?", "Alarm", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            player.Stop();
                            button1.Text = "OFF";
                            button1.Enabled = false;
                            button1.BackColor = Color.Red;
                            alarmOnOff = false;

                            if (checkBox2.Checked == false)
                            {
                                serialPort1.Write("0");
                            }

                        }
                        else
                        {
                            plusOne += 1;
                            dateTimePicker1.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, plusOne, 0);
                            if (checkBox1.Checked == true)
                            {
                                player.Stop();
                            }
                            textBox2.Text = dateTimePicker1.Value.ToShortTimeString();
                            timer2.Enabled = true;

                            if (checkBox2.Checked == false)
                            {
                                serialPort1.Write("0");
                            }
                        }
                    }
                        // not checkec, proceed without making noise
                    else
                    {
                        timer2.Enabled = false;
                        if (MessageBox.Show("Are you ready or not?", "Alarm", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                        {
                            button1.Text = "OFF";
                            button1.Enabled = false;
                            button1.BackColor = Color.Red;
                            alarmOnOff = false;

                            if (checkBox2.Checked == false)
                            {
                                serialPort1.Write("0");
                            }
                        }
                        else
                        {
                            plusOne += 1;
                            dateTimePicker1.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, plusOne, 0);
                            textBox2.Text = dateTimePicker1.Value.ToShortTimeString();
                            timer2.Enabled = true;

                            if (checkBox2.Checked == false)
                            {
                                serialPort1.Write("0");
                            }
                        }
                    }
                    
                }
            }
        }
        // open dialog and select a file
        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Select .wav files only!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            OpenFileDialog files = new OpenFileDialog();
            files.Filter = "WAV Files (.wav)|*.wav|All Files (*.*)|*.*";
            files.Multiselect = false;
            files.ShowDialog();
            textBox1.Text = files.FileName.ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            plusOne = dateTimePicker1.Value.Minute;
            plusOne += 1;
            dateTimePicker1.Value = new DateTime (DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, plusOne, 0);

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                timer3.Enabled = false;
                serialPort1.Write("0");
            }
        }

    }
}
